<?php
class Alumno extends AppModel{
    
    var $name = 'Alumno';    
       
    var $belongsTo = array(
            'Familia' => array(
            'className' => 'Familia',
                        'foreignKey' => 'familia_id'
            )
      );
    var $hasOne = array(
        'Tercero' => array(
            'className' => 'Tercero',
            'foreignKey' => 'id',
            'dependent' => false,
        )
    );
    
    function generarCodigo(){          
        $this->recursive = -1;
        $codigo = $this->find('first', array(
                         'fields' => array('MAX(codigo) as "Alumno__codigo"'),
                         'conditions'=>array('activo'=>true)
                         )
                  );

        if(!empty($codigo['Alumno']['codigo'])){
            $code = str_pad((int)$codigo['Alumno']['codigo'] + 1 ,4,'0',STR_PAD_LEFT); 
        }
        else{
            $code = '1'.str_pad('1',4,'0',STR_PAD_LEFT);          
        }
        return $code;
    }
    
}
?>